home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
cstdio.arc
/
SRC.ARC
/
PRINTF.C
< prev
next >
Wrap
C/C++ Source or Header
|
1984-12-10
|
3KB
|
153 lines
/* printf.c - formatted output conversion.
(C) Copyright 1984 Gregory R. Mansfield - All Rights Reserved.
G. R. Mansfield. 84/06/07.
Ver 1.2-4C10.
*/
#include <defstd.h>
#include <stdio.h>
#include <ctype.h>
printf(s, a) /* formatted output conversion to stdout */
char *s, *a;
{
_printf(stdout, s, &a);
}
fprintf(fp, s, a) /* formatted output conversion to stream */
FILE *fp;
char *s, *a;
{
_printf(fp, s, &a);
}
sprintf(d, s, a) /* formatted output conversion to string */
char *s, *a;
{
stdbfr._cnt = 32000;
stdbfr._ptr = d;
_printf(stdbfr, s, &a);
putc('\0', stdbfr);
}
_printf(fp, s, ap) /* output conversion formatter */
FILE *fp;
char *s, **ap;
{
char c, pad, *sp, st[128];
int l, left, maxchr, width;
long *lp;
while (*s) {
if (*s != '%') { /* copy text string */
if (fp != stdbfr && *s == '\n')
putc('\r', fp);
putc(*s++, fp);
}
else {
s++;
if (*s == '%') {
putc(*s++, fp);
continue;
}
sp = st;
left = maxchr = width = 0;
pad = ' ';
if (*s == '-') { /* left justified */
left = 1;
s++;
}
if (*s == '0') { /* '0' fill */
pad = '0';
s++;
}
if (isdigit(*s)) { /* width specification */
width = atoi(s);
while (isdigit(*s))
s++;
}
else if (*s == '*') { /* width from argument list */
width = *ap++;
s++;
}
if (*s == '.') { /* maximum field size */
s++;
if (isdigit(*s)) { /* from string */
maxchr = atoi(s);
while (isdigit(*s))
s++;
}
else if (*s == '*') { /* from argument list */
width = *ap++;
s++;
}
}
c = *s++;
if (c == 'l') {
c = toupper(*s);
s++;
}
switch (c) { /* convert argument */
case 'D': /* integer to decimal */
lp = ap++;
ltoa(*lp, sp);
break;
case 'O': /* integer to octal */
lp = ap++;
ltoab(*lp, sp, 8L);
break;
case 'U': /* unsigned to decimal */
lp = ap++;
ltoab(*lp, sp, 10L);
break;
case 'X': /* integer to hexadecimal */
lp = ap++;
ltoab(*lp, sp, 16L);
break;
case 'c': /* character */
*st = *ap;
st[1] = '\0';
break;
case 'd': /* integer to decimal */
itoa(*ap, sp);
break;
case 'o': /* integer to octal */
itoab(*ap, sp, 8);
break;
case 's': /* string */
sp = *ap;
break;
case 'u': /* unsigned to decimal */
itoab(*ap, sp, 10);
break;
case 'x': /* integer to hexadecimal */
itoab(*ap, sp, 16);
break;
default :
*st = '\0';
break;
}
ap++;
if (!(l = strlen(sp)) && c == 'c')
l++;
if (maxchr && maxchr < l)
l = maxchr;
width = (width > l) ? width - l : 0;
if (!left)
while (width--)
putc(pad, fp);
while (l--) {
if (fp != stdbfr && *sp == '\n')
putc('\r', fp);
putc(*sp++, fp);
}
if (left)
while (width--)
putc(pad, fp);
}
}
}